/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktemplate;

import java.util.ArrayList;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Josh
 */
public class ProjectileDynamite extends Collideable implements Projectile, Updateable, Renderable {

    private float speed;
    private float direction;
    private Object creator;
    private float damage;
    private float x, y;
    private Image sprite;
    public static ArrayList<ProjectileDynamite> all = new ArrayList<ProjectileDynamite>();
    
    public ProjectileDynamite(float x, float y) {
        this.x = x;
        this.y = y;
        all.add(this);
        try {
            sprite = new Image("sprites/dynamite.png"); //try to load the sprite
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
    }

    public ProjectileDynamite(float x, float y, float sp, float dir, float dam, Object cre) {
        this.x = x;
        this.y = y;
        speed = sp;
        direction = dir;
        damage = dam;
        creator = cre;
        all.add(this);
        try {
            sprite = new Image("sprites/dynamite.png"); //try to load the sprite
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
    }

    public float getSpeed() {
        return speed;
    }

    public float getDirection() {
        return direction;
    }

    public Object getCreator() {
        return creator;
    }

    public float getDamage() {
        return damage;
    }

    public boolean update(Input input, StateBasedGame sb) {
        x += Math.cos(direction) * speed;
        y += Math.sin(direction) * speed;
        sprite.setRotation(direction);
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
        for (int i = 0; i < Enemy1.all.size(); i++) {
        }
          boundingBox = new Rectangle(x, y, sprite.getWidth() +63, sprite.getHeight() +63);
        Enemy1 collide = (Enemy1) Collideable.getCollisionObject((Shape) boundingBox, Collideable.convertToCollidable(Enemy1.all));
        ArrayList<Enemy1> collides = (ArrayList<Enemy1>) Collideable.getCollisionObjects(boundingBox, Collideable.convertToCollidable(Enemy1.all));
        boundingBox = new Rectangle(x, y, sprite.getWidth() - 1, sprite.getHeight() - 1);
        if (!collides.isEmpty()) {
            for (int i = 0; i < collides.size(); i++) {
                collides.get(i).subHealth(damage);
            }
            return true;
        }
        if (x > sb.getContainer().getWidth() || y > sb.getContainer().getHeight() || x < 0 || y < 0) {
            return true;
        } else {
            return false;
        }
    }

    public void render(Graphics g) {
        sprite.drawCentered(x, y);
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void setDirection(float dir) {
        direction = dir;
    }

    public void setSpeed(float sp) {
        speed = sp;
    }

    public void setCreator(Object cre) {
        creator = cre;
    }

    public void setDamage(float dam) {
        damage = dam;
    }

    public void mousePressed(StateBasedGame sb, int button) {
    }
}
